<?php 
header('Content-Type: text/html; charset=iso-8859-1');
$wl = 500;
$hl = 5;
include ('../../Connections/KRAPPERCON.php');
require('../../Libs/fpdf/fpdf.php');
require('../../Libs/fpdf/extend.php');
$oper = getGet('oper');
//$oper = 'pdf';

if ($oper == 'pdf'){

$f1 = getGet('t_rc_fini');
$f2 = getGet('t_rc_ffin');

$sql = "call sp_getRegCompras('".$f1."','".$f2."');";

$year = substr($f1,0,4);
$month = substr($f1,5,2);
$nom_mes = nameMes($month);
$periodo = $month."/".$year;

$res = $mysqli->query($sql);
//doEcho($sql);
$fila = 0;
$start = 13;
$totneto = 0;
$tottot = 0;
$hl = 5;
$totiva = 0;
	
	$typ = 'I';
$sx = 5	;
$fnt = 'Arial';

if ($res){

//$pdf=new PDF('P','mm',array(225,222));
$pdf=new PDF('L','mm','A4');
$pdf->AddPage();
$pdf->AddFont('Dot','','dot.php');
$pdf->SetMargins(10,10,10);
$pdf->SetFont($fnt,'B',8);
$pdf->SetAutoPageBreak(true, 1); 
	
	if ($res->num_rows==0) {
	?>
    <script type="text/javascript">
	alert('No existe registros seleccionados.');
	window.close();
	</script>
    <?php 
	exit;
	}
	
	$ruc = '201312321321';
$pdf->SetFont($fnt,'B',12);
$pdf->SetX(5);
$pdf->Cell(100,$hl,"FORMATO 8.1: REGISTRO DE COMPRAS",0,1,"L");
$pdf->SetX(5);
$pdf->Cell(100,$hl,"PERIODO: ".$periodo,0,1,"L");
$pdf->SetX(5);
$pdf->Cell(100,$hl,"RUC: ".$ruc,0,1,"L");
$pdf->SetX(5);
$pdf->Cell(100,$hl+4,"INVERSIONES KRAPPER S.A.C.",0,1,"L");
	
	$pdf->SetFont($fnt,'B',8);
	$border = 1;
	//->setCellValue('C6', '4')->setCellValue('C7', '7')->setCellValue('C8', '1')->setCellValue('C9', '7');
	$cols = array(7,16,16,6,9,17,6,20,80,15,15,15,18,11);
		
	$pdf->Cell($cols[0],$hl,"Reg",$border,0,"L");
	$pdf->Cell($cols[1],$hl,"F.Emision",$border,0,"L");
	$pdf->Cell($cols[2],$hl,"F.Vencim",$border,0,"L");
	$pdf->Cell($cols[3],$hl,"Tip",$border,0,"L");
	$pdf->Cell($cols[4],$hl,"Serie",$border,0,"L");
	$pdf->Cell($cols[5],$hl,"Numero",$border,0,"L");
	$pdf->Cell($cols[6],$hl,"Tip",$border,0,"L");
	$pdf->Cell($cols[7],$hl,"Documento",$border,0,"L");
	$pdf->Cell($cols[8],$hl,"Raz.Social",$border,0,"L");
	$pdf->Cell($cols[9],$hl,"Base",$border,0,"L");
	$pdf->Cell($cols[10],$hl,"IGV",$border,0,"L");
	$pdf->Cell($cols[11],$hl,"No.Grav",$border,0,"L");
	$pdf->Cell($cols[12],$hl,"Total",$border,0,"L");
	$pdf->Cell($cols[13],$hl,"TC",$border,1,"L");
		 
		 $pdf->SetFont($fnt,'',8);
		 
		$facfolio = '';
		$tip_prov = '';
		$nom_prov = '';
		$f1 = '';
		$f2 = '';
		$an_dua = '';
		$f3 = '';
		while($row = $res->fetch_assoc()){
			//print_r($row);						
			//continue;
			$facfolio = $row['FacturaRef'];
			if ($facfolio==""){
			continue;
			}
			//echo $facfolio;
			$fila = $fila + 1;
			$start = $start + 1;
			$tipcli = '';
			$tip_doc = $row['TipDocu'];	
			$tip_nif = $row['TipoNIF'];
			$ftc = myRound($row['TipCam'],3);
			$exon = $row['MonExon'];
			
			
			
			$folio = array();
			$folio = explode("-",$facfolio);
			//print_r($folio);
			
			if (!isset($folio[0])){
			$f1 = '';
			}
			else{
			$f1 = $folio[0];
			}
			if (!isset($folio[1])){
			$f2 = '';
			}
			else{
			$f2 = $folio[1];			
			}
			if (!isset($folio[2])){
			$f3 = '';
			}
			else {
			$f3 = $folio[2];
			}
			
			//eho "<br>".$factfolio;
			
			if ($exon>0){
			$exon = $exon * $ftc;
			}
			
			$nif_prov = $row['ProveedNIF'];
			$nom_prov = $row['ProveedRaz'];
			$ftot = abs($row['FactImporte']) * $ftc;
			$fiva = abs($row['FactImpuesto']) * $ftc;
			$fneto = $ftot - $exon - $fiva;
			
			
			
			if ($ftc == 1){
			$ftc = '';
			}
			//doEcho($fneto);
			//exit;
			$totneto += $fneto;
			$totiva += $fiva;
			$tottot += $ftot;
			
			$fec_fac = $row['FactCompFeFc'];
			$fec_ven = $row['FactFecVen'];
			
			if (trim($fec_ven) == ''){
			$fec_ven = $fec_fac;
			}
			
			if ($tip_doc=='50'){
			$ann_dua = substr($fec_fac,0,4);
			}
			
			$pdf->Cell($cols[0],$hl,$fila,$border,0,"R");
			$pdf->Cell($cols[1],$hl,getFecPE($fec_fac),$border,0,"L");
			$pdf->Cell($cols[2],$hl,getFecPE($fec_ven),$border,0,"L");
			$pdf->Cell($cols[3],$hl,$tip_doc,$border,0,"L");
			$pdf->Cell($cols[4],$hl,$f2,$border,0,"L");
			$pdf->Cell($cols[5],$hl,$f3,$border,0,"L");
			$pdf->Cell($cols[6],$hl,$tip_nif,$border,0,"L");
			$pdf->Cell($cols[7],$hl,$nif_prov,$border,0,"L");
			$pdf->Cell($cols[8],$hl,$nom_prov,$border,0,"L");
				
			/*		
			if ($tip_doc == '50'){
			$sheet->setCellValue('L'.$start,$fiva); // Monto sin IGV
			$sheet->setCellValue('Q'.$start,$exon); // Monto sin IGV
			}
			elseif ($tip_doc == '91'){
			$sheet->setCellValue('H'.$start,'00'); // TipDoc Ide Proveedor
			$sheet->getStyle('H'.$start)->getNumberFormat()->setFormatCode('00');
			}	
			elseif ($tip_doc == 'CP'){
			$sheet->setCellValue('S'.$start,$fneto); // Monto sin IGV
			$sheet->setCellValue('L'.$start,$fiva);
			}		
			else {
			$sheet->setCellValue('K'.$start,$fneto); // Monto sin IGV
			$sheet->setCellValue('L'.$start,$fiva);
			}
			*/

		// $totot
	$tottot = $fneto + $fiva + $exon;
	$pdf->Cell($cols[9],$hl,myRound($fneto),$border,0,"R"); // Base Imp
	$pdf->Cell($cols[10],$hl,myRound($fiva),$border,0,"R"); // IGV
	$pdf->Cell($cols[11],$hl,myRound($exon),$border,0,"R"); // Exon
	$pdf->Cell($cols[12],$hl,myRound($tottot),$border,0,"R"); // 
	$pdf->Cell($cols[13],$hl,$ftc,$border,1,"R");	
	
	}
		
	// Cargar Data
	$res->close();
	$mysqli->check_result();
	
	// Output File
//exit;
	$pdf->Output('RegCompras.pdf','I');

}
}

?>